<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>yukari world</title>
<#include "/lib/base_source.ftl">
<style type="text/css">
</style>
</head>
<body>
<div class='whole-container'>
<#include "/lib/header.ftl">
    <div>
        <ul class="breadcrumb">
        	<li>兑换管理</li><span class="divider">/</span>
            <li>修改物品</li>
        </ul>
    </div>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="container span12">
            <#include "/lib/alert.ftl">

                <div class='main-content'>
                    <div class="well form-horizontal">
                        <div class='main-action'>
                        	<form id="updateForm" action="" method="post">
                                <input  type="hidden" name="id" value="${gfit.id}"/>
                        		<div class="control-group">
	                                <label class="control-label">物品名</label>
	                                <div class="controls">
                                        <#if manageAuthLv == 0>
                                            <input id="name" type="text" name="name" value="${gfit.name}" />
                                        <#else>
                                            ${gfit.name}
                                        <input id="name" type="hidden" name="name" value="${gfit.name}" />
                                        </#if>
	                                </div>
	                            </div>
                                <div class="control-group">
                                    <label class="control-label">物品类型</label>
                                    <div class="controls">
                                        <input id="type" type="radio" name="type" value="0" <#if gfit.type == 0>checked</#if>/>虚拟物品 &nbsp;&nbsp;&nbsp;&nbsp;
                                        <input id="type" type="radio" name="type" value="1" <#if gfit.type == 1>checked</#if>/>实体物品
                                    </div>
                                </div>
                        		<div class="control-group">
	                                <label class="control-label">价格</label>
	                                <div class="controls">
										<input id="price" type="text" name="price" value="${gfit.price}" />
	                                </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">库存</label>
                                    <div class="controls">
                                        <input id="num" type="text" name="num" value="${gfit.num}" />
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">图片</label>
                                    <div class="controls" id="imageDiv">
                                        <input id="imageFile" type="file"  onchange="uploadGfitImage()"/>
										<#if gfit.image != ''>
										    <img id="imageSrc" src="http://image.loveyukari.com/${gfit.image}">
                                        <#else>
										    <img id="imageSrc" src="" style="display: block">
                                        </#if>
                                        <span id="uploadStatus" style="color:red;"></span>
                                        <input id="image" type="hidden" name="image" value="${gfit.image}"/>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label"></label>
                                    <div class="controls">
                                        <a href="javascript:" onclick="updateGfit()" class="btn btn-sm btn-info">修改</a>
                                    </div>
                                </div>
                			</form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var uploadFlag = true;
    function uploadGfitImage(){
        var imageFile = $("#imageFile")[0].files[0]
        if (imageFile) {
            if (uploadFlag){
                uploadFlag = false;
                var formData = new FormData();
                formData.append("image",imageFile);
                formData.append("type","exchange/gfit");
                $.ajax({
                    url : "${rc.contextPath}/admin/upload/image",
                    type : 'POST',
                    data : formData,
                    processData : false,
                    contentType : false,
                    beforeSend:function(){
                        $("#uploadStatus").html("正在上传，请稍后...")
                    },
                    success : function(result) {
                        if (result.code == 0){
                            $("#imageSrc").attr("src","http://image.loveyukari.com/"+result.data);
                            $("#imageSrc").show();
                            $("#image").val(result.data);
                            $("#uploadStatus").html("");
                        }
                        uploadFlag = true;
                    },
                    error : function(responseStr) {
                        $("#uploadStatus").html("上传失败")
                        uploadFlag = true;
                    }
                });
            }
        }

    }


    var updateFlag = true;
    function updateGfit(){
        if (updateFlag){
            updateFlag = false;
            $.ajax({
                url : "${rc.contextPath}/admin/exchange/gfit/update",
                dataType : "json",
                data :  $("#updateForm").serialize(),
                type:'post',
                success:function(json){
                    if (json.code == 0){
                        location.href = "${rc.contextPath}/admin/exchange/gfit/list";
                    } else {
                        updateFlag = true;
                        alert("修改失败: "+json.message);
                    }
				}
			});
		}
    }

</script>
</body>
</html>